package com.bw.controller.process;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.material.PiMaterial;
import com.bw.entity.process.PiProcess;
import com.bw.resp.ResponseResult;
import com.bw.service.process.IPiProcessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxq
 * @since 2020-06-19
 */
@RestController
@RequestMapping("/process")
public class PiProcessController {

    @Autowired
    private IPiProcessService processService;

    @PostMapping("/list/{page}/{size}")
    public ResponseResult getList(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody PiProcess process){
        ResponseResult result = ResponseResult.SUCCESS();

        try {
            PageHelper.startPage(page, size);
            QueryWrapper queryWrapper = new QueryWrapper();
            if (null!=process.getName()){
                queryWrapper.like("name","%"+process.getName()+"%");
            }

            queryWrapper.eq("isdel",0);
            List<PiProcess> processs = processService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(processs);
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败:" + e.toString());
        }
        return result;
    }

    @PostMapping("/saveProcess")
    public ResponseResult save(@RequestBody PiProcess process){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            int unm = (int)((Math.random()*9+1)*100000);
            String h = "gx";
            process.setCode(h+unm);
            process.setIsdel(0);
            processService.save(process);
            result.setResult("添加成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("添加失败"+e.toString());
        }
        return  result;
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult del(@PathVariable long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiProcess process = processService.getById(id);
            process.setIsdel(1);
            processService.saveOrUpdate(process);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("isdel",0);
            List<PiProcess> list = processService.list(queryWrapper);
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("删除失败"+e.toString());
        }
        return  result;
    }

    @GetMapping("/get/{id}")
    public ResponseResult get(@PathVariable long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiProcess process =  processService.getById(id);
            result.setResult(process);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return  result;
    }

    @PostMapping("/save")
    public ResponseResult update(@RequestBody PiProcess process){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            processService.saveOrUpdate(process);
            result.setResult("修改成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }

    @PostMapping("getProcessList")
    public ResponseResult getProcessList(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            List<PiProcess> list = processService.list();
            result.setResult("查询成功");
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return  result;
    }
}
